//
//  RootViewController.m
//  TheVideoClip
//
//  Created by 北斗君合MacMini on 2018/3/29.
//  Copyright © 2018年 北斗君合MacMini. All rights reserved.
//

#import "RootViewController.h"

#import "RootTableViewCell.h"
#import "RootCollectionViewCell.h"

#import "RootViewModel.h"

#import "RootModel.h"

#import "GraduallyNavAnimation.h"
#import "MagnifyNavAnimation.h"

#define RootCollectionViewCellID @"RootCollectionViewCell"

@interface RootViewController () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UINavigationControllerDelegate> {
    BOOL _isListStyle;
    CGFloat _itemMargin;
    CGFloat _itemWidth;
    CGFloat _itemHeight;
    CGRect _selectTypeFrame;
    CGFloat _selectItemCornerRadius;
}

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UICollectionView *collectionView;

@property (strong, nonatomic) NSArray<RootModel *> *dataArray;

@property (nonatomic, weak) id animationDelegate;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.title = @"视频处理";
    _isListStyle = YES;
    _itemMargin = 10;
    _itemWidth = (kScreen_Width - _itemMargin * 3) * 0.5;
    _itemHeight = 70;
    
    _tableView = [[UITableView alloc] init];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.contentInset = UIEdgeInsetsMake(kStatusAndNavigationBarHeight, 0, 0, 0);
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    _dataArray = [RootViewModel getDataArray];
    [_tableView reloadData];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(_itemWidth, _itemHeight);
    layout.minimumLineSpacing = _itemMargin;
    layout.minimumInteritemSpacing = _itemMargin;
    layout.sectionInset = UIEdgeInsetsMake(0, _itemMargin, 0, _itemMargin);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.collectionViewLayout = layout;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.contentInset = UIEdgeInsetsMake(10 + kStatusAndNavigationBarHeight, 0, 0, 0);
    [self.view addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [_collectionView registerClass:[RootCollectionViewCell class] forCellWithReuseIdentifier:RootCollectionViewCellID];
    _collectionView.hidden = _isListStyle;
    [_collectionView reloadData];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"图形" style:UIBarButtonItemStylePlain target:self action:@selector(changeViewStyle)];
    [self.navigationItem setRightBarButtonItem:rightItem];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)changeViewStyle {
    if (_isListStyle) {
        self.navigationItem.rightBarButtonItem.title = @"列表";
        [_collectionView reloadData];
    } else {
        self.navigationItem.rightBarButtonItem.title = @"图形";
        [_tableView reloadData];
    }
    _tableView.hidden = _isListStyle;
    _isListStyle = !_isListStyle;
    _collectionView.hidden = _isListStyle;
}

#pragma mark UITableView delegate and dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RootTableViewCell *cell = [RootTableViewCell cellWithTableView:tableView];
    cell.model = _dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return _dataArray[indexPath.row].cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *vc = [[NSClassFromString(_dataArray[indexPath.row].className) alloc] init];
    vc.title = _dataArray[indexPath.row].title;
    self.navigationController.delegate = self;
    self.animationDelegate = vc;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark UICollectionView delegate and dataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RootCollectionViewCellID forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    RootCollectionViewCell *cell = (RootCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    _selectTypeFrame = [cell.titleLabel convertRect:cell.titleLabel.frame toView:self.view];
    _selectItemCornerRadius = cell.cornerRadius;
    UIViewController *vc = [[NSClassFromString(_dataArray[indexPath.row].className) alloc] init];
    vc.title = _dataArray[indexPath.row].title;
    self.navigationController.delegate = self;
    self.animationDelegate = vc;
    [self.navigationController pushViewController:vc animated:YES];
}

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    if (_isListStyle) {
        GraduallyNavAnimation *animation = [[GraduallyNavAnimation alloc] init];
        animation.delegate = self.animationDelegate;
        return animation;
    } else {
        MagnifyNavAnimation *animation = [[MagnifyNavAnimation alloc] init];
        animation.controlRect = _selectTypeFrame;
        animation.cornerRadius = _selectItemCornerRadius;
        animation.isPush = YES;
        animation.transitionType = operation;
        return animation;
    }
    return nil;
}

- (void)dealloc {
    if (_tableView) {
        _tableView.delegate = nil;
        _tableView.dataSource = nil;
    }
    if (_collectionView) {
        _collectionView.delegate = nil;
        _collectionView.dataSource = nil;
    }
    if (_animationDelegate) {
        _animationDelegate = nil;
    }
}

@end
